<?php

class Media extends Model_Abstract
{
	public function getHitId()
	{
		try
		{
			$result = $this->_redis->incr('global:id:hit');
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function addHit($key, $value)
	{
		try
		{
			$result = $this->_redis->hMset($key, $value);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function addToSession($session, $score, $hit)
	{
		try
		{
			$result = $this->_redis->zAdd($session, $score, $hit);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function saveSession($session)
	{
		try
		{
			$result = $this->_redis->sAdd("sessions", $session);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function addToTimeframe($time, $hit)
	{
		try
		{
			$result = $this->_redis->rPush("timeframe:".Util::getFrameTime($time), $hit);
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getCampaignById($camid)
	{
		try
		{
			$sql = "select * from campaigns where id=? limit 1";
			$result = $this->_connection->query($sql, array($camid));
			if (count($result) == 0)
			{
				return false;
			}
			else
			{
				return $result[0];
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getCreativesByOfferId($offerid)
	{
		try
		{
			$sql = "select * from creatives where id in (select cre_id from o_cre_refs where o_id=?)";
			$result = $this->_connection->query($sql, array($offerid));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getCreativeById($creative_id)
	{
		try
		{
			$sql = "select * from creatives where id=?";
			$result = $this->_connection->query($sql, array($creative_id));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getConvPosByOffer($offer_id)
	{
		try
		{
			$sql = "select conv_pos from offers where id=?";
			$result = $this->_connection->query($sql, array($offer_id));
			if($result)
				return $result[0]['conv_pos'];
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function getOfferType($offer_id)
	{
		try
		{
			$sql = "select type from offers where id=? limit 1";
			$result = $this->_connection->query($sql, array($offer_id));
			if($result)
				return $result[0]['type'];
			return false;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getLandingUrlsByOfferId($offerid)
	{
		try
		{
//			$sql = "select * from landing_urls where id in (select lu_id from o_lu_refs where o_id=?)";
			$sql = "select l.id as id, l.url as url, r.weight as weight from o_lu_refs r left join landing_urls l on r.lu_id=l.id where r.o_id=?";
			$result = $this->_connection->query($sql, array($offerid));
			return $result;
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}

	public function getLandingUrlById($id)
	{
		try
		{
			$sql = "select * from landing_urls where id=?";
			$result = $this->_connection->query($sql, array($id));
			if (count($result) > 0)
			{
				return $result[0]['url'];
			}
			else
			{
				return false;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	
	public function addIpLog($ip, $camid)
	{
		try
		{
			$sql = "insert into ip_logs set ip=?, updated=?, cam_id=?";
			$this->_connection->execute($sql, array($ip, time(), $camid));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getIpLog($ip, $camid)
	{
		try
		{
			$sql = "select * from ip_logs where ip=? and cam_id=? limit 1";
			$result = $this->_connection->query($sql, array($ip, $camid));
			if(count($result)>0)
			{
				return $result[0]['updated'];
			}
			else
			{
				return false;
			}
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function updateIpLog($ip, $camid)
	{
		try
		{
			$sql = "update ip_logs set updated=? where ip=? and cam_id=?";
			$this->_connection->execute($sql, array(time(), $ip, $camid));
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
}
?>